package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.StockDao;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.StockService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {

    @Autowired
    private StockDao stockDao;

    // 保存报损单 及报损单商品
    @Override
    @Transactional
    public void saveDamageListGoods(HttpSession session,DamageList damageList, String damageListGoodsStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        damageList.setDamageDate(sdf.format(new Date()));
        User user = (User)session.getAttribute("currentUser");
        damageList.setUserId(user.getUserId());
        stockDao.saveDamageList(damageList);
        Gson gson = new Gson();
        List<DamageListGoods> damageListGoodsList = gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>() {
        }.getType());
        for (DamageListGoods damageListGoods : damageListGoodsList) {
            damageListGoods.setDamageListId(damageList.getDamageListId());
            stockDao.saveDamageListGoods(damageListGoods);
        }
    }

    @Override
    public void saveOverflowListGoods(HttpSession session,OverflowList overflowList, String overflowListGoodsStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        overflowList.setOverflowDate(sdf.format(new Date()));
        User user = (User)session.getAttribute("currentUser");
        overflowList.setUserId(user.getUserId());
        stockDao.saveOverflowList(overflowList);
        Gson gson = new Gson();
        List<OverflowListGoods> overflowListGoodsList = gson.fromJson(overflowListGoodsStr, new TypeToken<List<OverflowListGoods>>() {
        }.getType());
        for (OverflowListGoods overflowListGoods : overflowListGoodsList) {
            overflowListGoods.setOverflowListId(overflowList.getOverflowListId());
            stockDao.saveOverflowListGoods(overflowListGoods);
        }
    }

    // 报损单查询
    @Override
    public List<DamageList> damageListGoods(String sTime, String eTime) {
        List<DamageList> damageListList = stockDao.damageListGoods(sTime,eTime);
        return damageListList;
    }

    // 查询报损单商品信息
    @Override
    public List<DamageListGoods> getDamageListGoodsList(Integer damageListId) {
        List<DamageListGoods> damageListGoodsList = stockDao.getDamageListGoodsList(damageListId);
        return damageListGoodsList;
    }

    // 报溢单查询
    @Override
    public List<OverflowList> overflowListGoods(String sTime, String eTime) {
        List<OverflowList> overflowListList = stockDao.overflowListGoods(sTime,eTime);
        return overflowListList;
    }

    @Override
    public List<OverflowListGoods> getOverflowListGoodsList(Integer overflowListId) {
        List<OverflowListGoods> overflowListGoodsList = stockDao.getOverflowListGoodsList(overflowListId);
        return overflowListGoodsList;
    }
}
